package com.etc.dao.impl;

import com.etc.dao.UserDao;
import com.etc.model.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserDaoImpl implements UserDao {
    private SqlSessionFactory sqlSessionFactory;
    public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }
    public Integer insert(User user) {
        SqlSession session = sqlSessionFactory.openSession();
        int insert = session.insert("insert", user);
        session.commit();
        session.close();
        return insert;
    }

    public Integer deleteById(int id) {
        SqlSession session = sqlSessionFactory.openSession();
        int deleteById = session.delete("deleteById", id);
        session.commit();
        session.close();
        return deleteById;
    }

    public User userById(int id) {
        SqlSession session = sqlSessionFactory.openSession();
        User userById = session.selectOne("userById", id);
        session.close();
        return userById;
    }

    public List<User> userByAll() {
            SqlSession session = sqlSessionFactory.openSession();
            List<User> userByAll = session.selectList("userByAll");
            session.close();
            return userByAll;
    }
}
